<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.moldCode }}
        </a>
      </template>
      <template #toolbar>
        <a-button type="primary" ghost @click="handleExportExcel()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsUseAsdMoldUseList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { asdMoldUseDelete, asdMoldUseListData } from '/@/api/mold/use/asdMoldUse';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { basMoldClsTreeData } from '/@/api/bas/mold/basMoldCls';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { parse } from 'crypto-js/enc-utf8';

  const { t } = useI18n('use.asdMoldUse');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('使用记录管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工装代码'),
        field: 'basMold.caddcode',
        component: 'Input',
      },
      {
        label: t('工装编码'),
        field: 'moldCode',
        component: 'Input',
      },
      {
        label: t('工装名称'),
        field: 'basMold.name',
        component: 'Input',
      },
      {
        label: t('工装类型'),
        field: 'basMold.basMoldCls.code',
        fieldLabel: 'basMold.basMoldCls.name',
        component: 'TreeSelect',
        componentProps: {
          api: basMoldClsTreeData,
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('工序编码'),
        field: 'operCode',
        component: 'Input',
      },
      {
        label: t('存货代码'),
        field: 'basInv.cinvaddcode',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'invCode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
      {
        label: t('设备名称'),
        field: 'devName',
        component: 'Input',
      },
      {
        label: t('加工日期'),
        field: 'endDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
    ],
    fieldMapToTime: [['endDate', ['endDate_gte', 'endDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('工装代码'),
      dataIndex: 'basMold.caddcode',
      key: 'mold.caddcode',
      sorter: true,
      width: 130,
      align: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('工装编码'),
      dataIndex: 'moldCode',
      key: 'a.mold_code',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('工装尾号'),
      dataIndex: 'lastNum',
      key: 'a.lastNum',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('工装名称'),
      dataIndex: 'basMold.name',
      key: 'mold.name',
      width: 130,
      align: 'left',
    },
    {
      title: t('工装类型'),
      dataIndex: 'basMold.basMoldCls.name',
      key: 'mold.cls_id',
      width: 80,
      align: 'left',
    },
    {
      title: t('工序'),
      dataIndex: 'operCode',
      key: 'a.oper_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货代码'),
      dataIndex: 'basInv.cinvaddcode',
      // key: 'a.inv_code',
      // sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'a.inv_code',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('生产线'),
      dataIndex: 'cline',
      key: 'a.cline',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('设备名称'),
      dataIndex: 'devName',
      key: 'a.dev_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('加工次数'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('累次使用次数'),
      dataIndex: 'basMold.useQty',
      key: 'mold.use_qty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('剩余寿命'),
      dataIndex: 'basMold.iqty',
      key: 'mold.iqty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('开始时间'),
      dataIndex: 'stratDate',
      key: 'a.strat_date',
      sorter: true,
      width: 130,
      align: 'center',
      ifShow: false,
    },
    {
      title: t('加工日期'),
      dataIndex: 'endDate',
      key: 'a.end_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('操作人'),
      dataIndex: 'user.userName',
      // key: 'user.userName',
      // sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注信息'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑使用记录'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'use:asdMoldUse:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除使用记录'),
        popConfirm: {
          title: t('是否确认删除使用记录'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'use:asdMoldUse:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: asdMoldUseListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await asdMoldUseDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  function handleExportExcel() {
    const { ctxAdminPath } = useGlobSetting();
    console.log(getForm().getFieldsValue());
    // const parse = ref<string>('');
    // for (const key in getForm().getFieldsValue()) {
    //     console.log(key, getForm().getFieldsValue()[key]);
    //     parse.value += key + '=' + getForm().getFieldsValue()[key] + '&';
    // }

    downloadByUrl({
      url: ctxAdminPath + '/mold/use/asdMoldUse/exportData',
      params: getForm().getFieldsValue(),
      target: '_self',
    });
  }
</script>
